<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once("Gecko/Form/Exception.php");
require_once("Gecko/Form/Field.php");

/**
 * Select Control with independent DataSource
 *
 * @package Gecko.Form.Field;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_Form_Field_Select extends Gecko_Form_Field {
	/**
	 * Data Source for Control
	 *
	 * @see Gecko_DataSource_Select_Interface
	 * @var unknown_type
	 */
	protected $source = null;

	/**
	 * The Total number of Elements
	 *
	 * @var int
	 */
	protected $numElements = 0;

	/**
	 * The Selected Label
	 *
	 * @var string
	 */
	protected $selectedLabel = null;

	/**
	 * The controlData after binding (a array)
	 *
	 * @var array
	 */
	protected $controlData = array();

	/**
	 * Control Type
	 *
	 * @var unknown_type
	 */
	protected $type = 'select';

	/**
	 * Creates a new Select box control
	 *
	 * @param string $name
	 * @param string $value
	 * @param array $params
	 */
	public function __construct($name, $value = "", $params = array() ) {
		parent::__construct( $name, $value, $params );

		$source = ( isset( $params['source'] ) ? $params['source'] : null );
		if( $source ) {
			$this->setSource( $source );
			$this->bind();
		}
	}

	/**
	 * Binds the DataSource to the control, you must call
	 * this function after setSource
	 *
	 * @access public
	 */
	public function bind() {
		$this->controlData = $this->source->getSelectData();
		$this->numElements = count( $this->controlData );
	}

	/**
	 * Returns the control Value
	 *
	 * @return string
	 */
	public function getValue() {
		$value = trim(parent::getValue());
		$params = $this->getParams();

		if( !empty( $params['emptyLabel'] ) && ( trim( $params['emptyLabel'] ) == $value ) ) {
			return "";
		} else {
			return $value;
		}
	}

	/**
	 * Sets the control DataSource
	 *
	 * @see Gecko_DataSource_Select_Interface
	 * @param Gecko_DataSource_Select_Interface $source
	 */
	public function setSource( Gecko_DataSource_Select_Interface $source ) {
		$this->source = $source;
	}

	/**
	 * Returns the total number of elements
	 *
	 * @return int
	 */
	public function getTotalElements() {
		return $this->numElements;
	}

	/**
	 * Returns the current selected label
	 *
	 * @return string
	 */
	public function getSelectedLabel() {
		return $this->controlData[$this->getValue()];
	}

	/**
	 * Returns the HTML rendered control
	 *
	 * @return string
	 */
	public function getControl() {
		$params = $this->getParams();
		$value = $this->getValue();
		$name = $this->getName();
		$vars = array();
		$template = Gecko_Router::LIBRARY_DIR . "/Assets/Form/Templates/Select.php";

		$vars['data'] = $this->controlData;
		$vars['name'] = $name;
		$vars['selected'] = $value;
		$vars['emptyLabel'] = $params['emptyLabel'];
		if(isset($params['onchange'])) {
			$vars['onchange'] = "onchange=\"{$params['onchange']}\"";
		}
		if(isset($params['onblur'])) {
			$vars['onblur'] = "onblur=\"{$params['onblur']}\"";
		}

		$html = Gecko_Template::renderTemplate( $template, $vars, true );
		return $html;
	}
}